package lib

import (
	"io"
	"net/http"
	"io/ioutil"
	"github.com/gin-gonic/gin"
)

func HttpRequest(url, requestType string, data io.Reader, header http.Header) []byte {
	req, err := http.NewRequest(requestType, url, data)
	if err != nil {
		HandleError("lib","HttpRequest",err)
		return nil
	}
	req.Header = header
	client := &http.Client{}
	res, err := client.Do(req)
	if err != nil {
		HandleError("lib","HttpRequest",err)
		return nil
	}
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		HandleError("lib","HttpRequest",err)
		return nil
	}
	return body
}


func GetParams(c *gin.Context) map[string]string {
	msName := c.Param("ms")
	apiName := c.Param("api")
	r := c.Request
	r.ParseForm()
	params := make(map[string]string)
	params["MS"] = msName
	params["API"] = apiName
	if r.Method == "GET" {
		for k, v := range r.Form {
			if c.Query(k) != "" {
				params[k] = v[0]
			}
		}
	} else {
		for k, v := range r.PostForm {
			params[k] = v[0]
		}
	}
	return params
}